// ------------------------------------------------------------------------------------
// SOUL_MV Destination Cursor.js
// ------------------------------------------------------------------------------------
/*:
* @plugindesc Changes your white blinking cursor into an image during mouse movement.
* @author Soulpour777 - soulxregalia.wordpress.com
* 
* @help

SOUL_MV Destination Cursor.js
Author: Soulpour777

This plugin does not have any Plugin Commands.

All images must be inside img / system folder.

* @param Cursor Image
* @desc Image name of the cursor sprite you are using. (img / system)
* @default destination_cursor
*
* @param Cursor Width
* @desc Width of the cursor.
* @default 48
*
* @param Cursor Height
* @desc Height of the cursor.
* @default 48
*
* @param Blink Speed
* @desc Speed of the blinking behavior for the cursor.
* @default 20
*
* @param Fade Effect
* @desc Do you want to use the fade in / fade out effect in the cursor when active?
* @default false
*
* @param Fade Speed
* @desc Speed of the fade behavior for the cursor.
* @default 20
*
* @param Scale Value
* @desc Scale value of the cursor when it appears. (1 + this._frameCount / Scale Value).
* @default 20
*
*/
Game_AnalogMove.prototype.canvasToMapX = function (x) {
    var tileWidth = $gameMap.tileWidth();
    var originX = $gameMap._displayX * tileWidth;
	var camScaleX = 1 + $gameSystem._drill_LCa_sX.cur;
	var scaledTouchInputX = x/camScaleX - (Graphics.boxWidth / camScaleX - Graphics.boxWidth)/2;
    var mapX = (originX + scaledTouchInputX) / tileWidth;
    return $gameMap.roundX(mapX);
};
Game_AnalogMove.prototype.canvasToMapY = function (y) {
    var tileHeight = $gameMap.tileHeight();
    var originY = $gameMap._displayY * tileHeight;
	var camScaleY = 1 + $gameSystem._drill_LCa_sY.cur;
	var scaledTouchInputY = y/camScaleY - (Graphics.boxHeight / camScaleY - Graphics.boxHeight)/2;
    var mapY = (originY + scaledTouchInputY) / tileHeight;
    return $gameMap.roundY(mapY);
};


Game_Map.prototype.canvasToMapX = function(x) {
    var tileWidth = this.tileWidth();
    var originX = this._displayX * tileWidth;
	var camScaleX = 1 + $gameSystem._drill_LCa_sX.cur;
	var scaledTouchInputX = x/camScaleX - (Graphics.boxWidth / camScaleX - Graphics.boxWidth)/2;
    var mapX = Math.floor((originX + scaledTouchInputX) / tileWidth);
    return this.roundX(mapX);
};
Game_Map.prototype.canvasToMapY = function(y) {
    var tileHeight = this.tileHeight();
    var originY = this._displayY * tileHeight;
	var camScaleY = 1 + $gameSystem._drill_LCa_sY.cur;
	var scaledTouchInputY = y/camScaleY - (Graphics.boxHeight / camScaleY - Graphics.boxHeight)/2;
    var mapY = Math.floor((originY + scaledTouchInputY) / tileHeight);
    return this.roundY(mapY);
};

(function(){
	var SOUL_MV = SOUL_MV || {};
	SOUL_MV.DestinationCursor = {};
	SOUL_MV.DestinationCursor.cursorImage = PluginManager.parameters('SOUL_MV Destination Cursor')['Cursor Image'] || 'destination_cursor';
	SOUL_MV.DestinationCursor.cursorWidth = Number(PluginManager.parameters('SOUL_MV Destination Cursor')['Cursor Width'] || 48);
	SOUL_MV.DestinationCursor.cursorHeight = Number(PluginManager.parameters('SOUL_MV Destination Cursor')['Cursor Height'] || 48);
	SOUL_MV.DestinationCursor.blinkSpeed = Number(PluginManager.parameters('SOUL_MV Destination Cursor')['Blink Speed'] || 20);
	SOUL_MV.DestinationCursor.horzScale = Number(PluginManager.parameters('SOUL_MV Destination Cursor')['Scale Value'] || 20);
	SOUL_MV.DestinationCursor.fadeSpeed = Number(PluginManager.parameters('SOUL_MV Destination Cursor')['Fade Speed'] || 20);
	SOUL_MV.DestinationCursor.fadeEffect = PluginManager.parameters('SOUL_MV Destination Cursor')['Fade Effect'] === "true" ? true : false;
	Sprite_Destination.prototype.createBitmap = function() {
	    var tileWidth = SOUL_MV.DestinationCursor.cursorWidth;
	    var tileHeight = SOUL_MV.DestinationCursor.cursorHeight;
	    this.bitmap = new Bitmap(tileWidth, tileHeight);
	    this.bitmap = ImageManager.loadSystem(SOUL_MV.DestinationCursor.cursorImage);
	    this.anchor.x = 0.5;
	    this.anchor.y = 0.5;
	    this.blendMode = Graphics.BLEND_ADD;
	};

	Sprite_Destination.prototype.update = function() {
		Sprite.prototype.update.call(this);
		// 在移动端时永久显示目的地光标。在PC端时，仅在鼠标未点击时（拖拽后释放）显示目的地光标。
		if ($gameTemp.isDestinationValid() && (!Utils.isNwjs() || !TouchInput.isPressed())){
			this.updatePosition();
			this.updateAnimation();
			this.visible = true;
		} else {
			this._frameCount = 0;
			this.visible = false;
		}
	};

	Sprite_Destination.prototype.updateAnimation = function() {
	    this._frameCount++;
	    this._frameCount %= SOUL_MV.DestinationCursor.blinkSpeed;
	    if (SOUL_MV.DestinationCursor.fadeEffect) {
	    	this.opacity = (SOUL_MV.DestinationCursor.fadeSpeed - this._frameCount) * 6;
	    }
	    this.scale.x = (1 + this._frameCount / SOUL_MV.DestinationCursor.horzScale);
	    this.scale.y = this.scale.x;
		this.scale.x /= (1 + $gameSystem._drill_LCa_sX.cur);	// SWDA：镜头缩放不改变光标大小）
		this.scale.y /= (1 + $gameSystem._drill_LCa_sY.cur);	// SWDA：镜头缩放不改变光标大小）
	};
	
// SWDA：改写processMapTouch，以像素精度记录目的地实际位置，同时考虑镜头缩放问题。
SOUL_MV.DestinationCursor.Scene_Map_processMapTouch = Scene_Map.prototype.processMapTouch;
Scene_Map.prototype.processMapTouch = function() {
    if (TouchInput.isTriggered() || this._touchCount > 0) {
        if (TouchInput.isPressed()) {
            if (this._touchCount === 0 || this._touchCount >= 15) {
				var tileWidth = $gameMap.tileWidth();
				var originX = $gameMap._displayX * tileWidth;
				var camScaleX = 1 + $gameSystem._drill_LCa_sX.cur;
				var scaledTouchInputX = TouchInput.x/camScaleX - (Graphics.boxWidth / camScaleX - Graphics.boxWidth)/2;
				var mapX = (originX + scaledTouchInputX) / tileWidth;
				var realDestinationX = $gameMap.roundX(mapX);

					
				var tileHeight = $gameMap.tileHeight();
				var originY = $gameMap._displayY * tileHeight;
				var camScaleY = 1 + $gameSystem._drill_LCa_sY.cur;
				var scaledTouchInputY = TouchInput.y/camScaleY - (Graphics.boxHeight / camScaleY - Graphics.boxHeight)/2;
				var mapY = (originY + scaledTouchInputY) / tileHeight;
				var realDestinationY = $gameMap.roundY(mapY);

				$gameTemp._realDestinationX = realDestinationX;
                $gameTemp._realDestinationY = realDestinationY;

            }
        }
    }
	SOUL_MV.DestinationCursor.Scene_Map_processMapTouch.call(this);
};
// SWDA：重写updatePosition，用像素精度目的地坐标计算目的地光标位置，同时考虑镜头缩放问题。
Sprite_Destination.prototype.updatePosition = function() {
		var tileWidth = $gameMap.tileWidth();
		var tileHeight = $gameMap.tileHeight();
		//var x = $gameTemp.destinationX();
		//var y = $gameTemp.destinationY();
		var x = $gameTemp._realDestinationX;
		var y = $gameTemp._realDestinationY;
		this.x = ($gameMap.adjustX(x)) * tileWidth;
		this.y = ($gameMap.adjustY(y)) * tileHeight;
	};

})();